package com.yifeng.repo.apm.endpoint.utils;

import org.apache.skywalking.apm.toolkit.trace.TraceContext;
import org.slf4j.MDC;

import java.util.Map;

/**
 * Created by daibing on 2023/3/18.
 */
public class BaseUtil {
    public static final String TRACE_ID = "X-Trace-Id";
    public static final String FRONTEND_ID = "X-Frontend-Id";

    public static String getTraceId() {
        if (isNotBlank(TraceContext.traceId())) {
            return TraceContext.traceId();
        }
        if (isNotBlank(MDC.get(TRACE_ID))) {
            return MDC.get(TRACE_ID);
        }
        return "NO_TID";
    }

    public static String getFrontendId() {
        if (isNotBlank(MDC.get(FRONTEND_ID))) {
            return MDC.get(FRONTEND_ID);
        }
        return "NO_FID";
    }

    public static boolean isBlank(String s) {
        return s == null || s.trim().length() == 0;
    }

    public static boolean isNotBlank(String s) {
        return s != null && s.trim().length() > 0;
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return null == map || map.isEmpty();
    }

    public static boolean exists(String clz) {
        try {
            Class.forName(clz);
        } catch (ClassNotFoundException e) {
            return false;
        }
        return true;
    }
}
